package com.ly.pj.module.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

//@ControllerAdvice//次注解描述的类为spring web模块的全局异常处理类
//@ResponseBody
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * @ExceptionHandler此注解描述的方法为spring web模块中的异常处理方法
     * @param e 算数异常对象
     * @return 提示字符串+异常信息
     */
    @ExceptionHandler(ArithmeticException.class)
    //@ResponseBody
    public String doHandleArithmeticException(ArithmeticException e){
        e.printStackTrace();
        System.out.println("GlobalExceptionHandler.doHandleArithmeticException()");
        return "计算过程中出现了异常，异常信息为："+e.getMessage();
    }
    @ExceptionHandler(RuntimeException.class)
    //@ResponseBody
    public String doHandleRuntimeException(RuntimeException e){
        e.printStackTrace();
        System.out.println("GlobalExceptionHandler.doHandleRuntimeException()");
        return "运行过程中出现了异常，异常信息为："+e.getMessage();
    }
    @ExceptionHandler(Throwable.class)
    //@ResponseBody
    public String doHandleThrowable(Throwable e){
        e.printStackTrace();
        System.out.println("GlobalExceptionHandler.doHandleThrowable()");
        //发邮件，发短信，服务器后台报警（播放一段音乐）
        return "系统维护中";
    }
}
